package starsservice.com.kc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import starsservice.com.kc.Util.PasswordUtil;
import starsservice.com.kc.bean.Account;
import starsservice.com.kc.bean.AccountPassowrd;
import org.springframework.stereotype.Service;
import starsservice.com.kc.mapper.AccountPassowrdMapper;
import starsservice.com.kc.service.IAccountPassowrdService;

import javax.annotation.Resource;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.function.Consumer;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-12-13
 */
@Service
public class AccountPassowrdServiceImpl extends ServiceImpl<AccountPassowrdMapper, AccountPassowrd> implements IAccountPassowrdService {

    @Resource
    private AccountPassowrdMapper accountPassowrdMapper;

    @Override
    public int InsterAccountPassword(String password, Account account) throws NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
        System.out.println(password);
        String newpassowrd = PasswordUtil.encryptAES(password.getBytes());
        System.out.println(newpassowrd);
        AccountPassowrd accountPassowrd = new AccountPassowrd();
        accountPassowrd.setJobNumber(account.getAccountJobNumber());
        accountPassowrd.setPhone(account.getAccountPhone());
        accountPassowrd.setAccountPassowrd(newpassowrd);
        return accountPassowrdMapper.insert(accountPassowrd);
    }

    @Override
    public AccountPassowrd selectByphone(String phone,String password) {
        System.out.println("jinlai;e");
        return accountPassowrdMapper.selectByphone(Wrappers.<AccountPassowrd>lambdaQuery()
                .eq(AccountPassowrd::getPhone,phone).eq(AccountPassowrd::getAccountPassowrd,password));
    }

    @Override
    public AccountPassowrd selectByjobnumber(String jobnumber, String password) {
        return accountPassowrdMapper.selectByphone(Wrappers.<AccountPassowrd>lambdaQuery().
                eq(AccountPassowrd::getJobNumber,jobnumber)
                .and(AccountPassword->Wrappers.<AccountPassowrd>lambdaQuery().eq(AccountPassowrd::getAccountPassowrd,password)));
    }
}
